<?php


namespace App\Extensions\Exporters;


use Encore\Admin\Grid;
use Encore\Admin\Grid\Exporters\ExcelExporter;
use Maatwebsite\Excel\Concerns\WithMapping;

class TransverseExporter extends ExcelExporter implements WithMapping
{
    protected $fileName = '横向课题.xlsx';
    protected $headings = [
        '系统编号',
        '合同登记号',
        '项目名称',
        '合同类型',
        '合作单位',
        '合同金额（万元）',
        '到账金额（万元）',
        '所属领域',
        '签订时间',
        '签订地点',
        '项目负责人',
        '科室',
        '研究状态',
        '结项时间',
        '创建人',
        '创建时间'
    ];

    protected $columns = [
        'uuid'          => '系统编号',
        'contract_no'   => '合同登记号',
        'name'          => '项目名称',
        'contract_type' => '合同类型',
        'buyer'         => '合作单位',
        'amount'        => '合同金额（万元）',
        'in_amount'     => '到账金额（万元）',
        'domain'        => '所属领域',
        'sign_date'     => '签订时间',
        'sign_address'  => '签订地点',
        'user_name'     => '项目负责人',
        'department_id' => '科室',
        'state'         => '研究状态',
        'review_status' => '审批状态',
        'end_time'      => '结项时间',
        'creator_id'    => '创建人',
        'created_at'    => '创建时间',
    ];

    public function __construct(Grid $grid = null)
    {
        parent::__construct($grid);
    }

    public function map($row): array
    {
        if ($row->review_status == 1) {
            $state_label = data_get(['在研', '已结项'], $row->state, '');
        } else {
            $state_label = '';
        }
        return [
            $row->uuid . "\t",
            $row->contract_no . "\t",
            $row->name,
            $row->contract_type,
            $row->buyer,
            $row->amount,
            $row->in_amount,
            $row->domain,
            $row->sign_date,
            $row->sign_address,
            $row->user_name,
            $row->department->name ?? '',
            $state_label,
            $row->end_time,
            $row->creator->name ?? '',
            $row->created_at,
        ];
    }

    public function setFileName($name)
    {
        $this->fileName = $name . '.xlsx';
        return $this;
    }

    public function headings(): array
    {
        return $this->headings;
    }


}
